#include "family.h"

// 读取文件
void FamilyTree::readCSV(std::string filename)
{
    std::ifstream file(filename);
    if (!file.is_open())
    {
        std::cerr << "无法打开文件: " << filename << std::endl;
        return;
    }
    std::string line;
    std::getline(file, line); // 跳过标题行

    // std::vector<FamilyMember *> members; // 用于存储所有成员

    while (std::getline(file, line))
    {
        std::stringstream ss(line);
        std::string name, birthDate, contactInfo, isMarried, isAlive, deathDate, fatherName, motherName;

        std::getline(ss, name, ',');
        std::getline(ss, birthDate, ',');
        std::getline(ss, contactInfo, ',');
        std::getline(ss, isMarried, ',');
        std::getline(ss, isAlive, ',');
        std::getline(ss, deathDate, ',');
        std::getline(ss, fatherName, ',');
        std::getline(ss, motherName, ',');

        bool married = (isMarried == "Yes");
        bool alive = (isAlive == "Yes");

        FamilyMember *member = new FamilyMember(name, birthDate, contactInfo, married, alive, deathDate, fatherName, motherName); // 构建成员

        FamilyMember *parent = findMemberByName(fatherName); // 假设使用父亲的名字来建立父子关系
        if (parent == nullptr)
        {
            parent = findMemberByName(motherName); // 如果父亲不存在，尝试使用母亲的名字
        }
        addMember(parent, member);
    }
    // 打印录入成功
    std::cout << "录入成功" << std::endl;
    file.close();

    // // 在读取完所有成员后，构建父子关系
    // for (auto member : members)
    // {
    //     FamilyMember *parent = findMemberByName(member->fatherName);
    //     addMember(parent, member);
    // }
}